|
1
|
|
|
/* global $ */ |
|
2
|
|
|
/* global Craft */ |
|
3
|
|
|
/* global Garnish */ |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Image Optimize plugin for Craft CMS |
|
7
|
|
|
* |
|
8
|
|
|
* OptimizedImages Field JS |
|
9
|
|
|
* |
|
10
|
|
|
* @author nystudio107 |
|
11
|
|
|
* @copyright Copyright (c) 2017 nystudio107 |
|
12
|
|
|
* @link https://nystudio107.com |
|
13
|
|
|
* @package ImageOptimize |
|
14
|
|
|
* @since 1.2.0 |
|
15
|
|
|
*/ |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Convert the passed in bytes into a human readable format |
|
19
|
|
|
* |
|
20
|
|
|
* @param bytes |
|
|
|
|
|
|
21
|
|
|
* @param si |
|
|
|
|
|
|
22
|
|
|
* @param dp |
|
|
|
|
|
|
23
|
|
|
* @returns {string} |
|
|
|
|
|
|
24
|
|
|
*/ |
|
25
|
|
|
function humanFileSize(bytes, si=false, dp=1) { |
|
|
|
|
|
|
26
|
|
|
const thresh = si ? 1000 : 1024; |
|
27
|
|
|
|
|
28
|
|
|
if (Math.abs(bytes) < thresh) { |
|
29
|
|
|
return bytes + ' B'; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
const units = si |
|
33
|
|
|
? ['kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'] |
|
34
|
|
|
: ['KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB']; |
|
35
|
|
|
let u = -1; |
|
36
|
|
|
const r = 10**dp; |
|
37
|
|
|
|
|
38
|
|
|
do { |
|
39
|
|
|
bytes /= thresh; |
|
40
|
|
|
++u; |
|
41
|
|
|
} while (Math.round(Math.abs(bytes) * r) / r >= thresh && u < units.length - 1); |
|
42
|
|
|
|
|
43
|
|
|
|
|
44
|
|
|
return bytes.toFixed(dp) + ' ' + units[u]; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* After an image has loaded, query the performance API for the decodedBodySize |
|
49
|
|
|
* |
|
50
|
|
|
* @param image |
|
51
|
|
|
*/ |
|
52
|
|
|
function imageLoaded(image) { |
|
|
|
|
|
|
53
|
|
|
const url = image.src || image.href; |
|
54
|
|
|
if (url && url.length > 0) { |
|
55
|
|
|
const iTime = performance.getEntriesByName(url)[0]; |
|
|
|
|
|
|
56
|
|
|
if (iTime !== undefined) { |
|
57
|
|
|
const elem = image.parentNode.parentNode.parentNode.nextElementSibling.querySelector('.io-file-size'); |
|
58
|
|
|
if (elem) { |
|
59
|
|
|
elem.innerHTML = humanFileSize(iTime.decodedBodySize, true); |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
(function ( $, window, document) { |
|
66
|
|
|
|
|
67
|
|
|
const pluginName = "ImageOptimizeOptimizedImages", |
|
68
|
|
|
defaults = { |
|
69
|
|
|
}; |
|
|
|
|
|
|
70
|
|
|
|
|
71
|
|
|
// Plugin constructor |
|
72
|
|
|
function Plugin( element, options ) { |
|
|
|
|
|
|
73
|
|
|
this.element = element; |
|
74
|
|
|
|
|
75
|
|
|
this.options = $.extend( {}, defaults, options) ; |
|
|
|
|
|
|
76
|
|
|
|
|
77
|
|
|
this._defaults = defaults; |
|
78
|
|
|
this._name = pluginName; |
|
79
|
|
|
|
|
80
|
|
|
this.init(); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
Plugin.prototype = { |
|
84
|
|
|
|
|
85
|
|
|
init: function() { |
|
|
|
|
|
|
86
|
|
|
$(function () { |
|
|
|
|
|
|
87
|
|
|
|
|
88
|
|
|
/* -- _this.options gives us access to the $jsonVars that our FieldType passed down to us */ |
|
89
|
|
|
|
|
90
|
|
|
const images = document.querySelectorAll("img.io-preview-image"); |
|
91
|
|
|
for (const image of images) { |
|
92
|
|
|
if (image.complete) { |
|
93
|
|
|
imageLoaded(image); |
|
94
|
|
|
} else { |
|
95
|
|
|
image.addEventListener('load', (event) => { |
|
|
|
|
|
|
96
|
|
|
imageLoaded(event.target); |
|
97
|
|
|
}); |
|
|
|
|
|
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
}); |
|
|
|
|
|
|
101
|
|
|
} |
|
102
|
|
|
}; |
|
103
|
|
|
|
|
104
|
|
|
// A really lightweight plugin wrapper around the constructor, |
|
105
|
|
|
// preventing against multiple instantiations |
|
106
|
|
|
$.fn[pluginName] = function ( options ) { |
|
107
|
|
|
return this.each(function () { |
|
|
|
|
|
|
108
|
|
|
if (!$.data(this, "plugin_" + pluginName)) { |
|
109
|
|
|
$.data(this, "plugin_" + pluginName, |
|
|
|
|
|
|
110
|
|
|
new Plugin( this, options )); |
|
|
|
|
|
|
111
|
|
|
} |
|
112
|
|
|
}); |
|
|
|
|
|
|
113
|
|
|
}; |
|
114
|
|
|
|
|
115
|
|
|
})($, window, document); |
|
116
|
|
|
|
|
117
|
|
|
Craft.OptimizedImagesInput = Garnish.Base.extend( |
|
118
|
|
|
{ |
|
119
|
|
|
id: null, |
|
120
|
|
|
inputNamePrefix: null, |
|
121
|
|
|
inputIdPrefix: null, |
|
122
|
|
|
|
|
123
|
|
|
$container: null, |
|
124
|
|
|
$blockContainer: null, |
|
125
|
|
|
$addBlockBtnContainer: null, |
|
126
|
|
|
$addBlockBtnGroup: null, |
|
127
|
|
|
$addBlockBtnGroupBtns: null, |
|
128
|
|
|
|
|
129
|
|
|
blockSort: null, |
|
130
|
|
|
blockSelect: null, |
|
131
|
|
|
|
|
132
|
|
|
init: function(id, inputNamePrefix) { |
|
|
|
|
|
|
133
|
|
|
|
|
134
|
|
|
this.id = id; |
|
135
|
|
|
this.inputNamePrefix = inputNamePrefix; |
|
136
|
|
|
this.inputIdPrefix = Craft.formatInputId(this.inputNamePrefix); |
|
137
|
|
|
|
|
138
|
|
|
this.$container = $('#' + this.id); |
|
139
|
|
|
this.$blockContainer = this.$container.children('.variant-blocks'); |
|
140
|
|
|
this.$addBlockBtnContainer = this.$container.children('.buttons'); |
|
141
|
|
|
this.$addBlockBtnGroup = this.$addBlockBtnContainer.children('.btngroup'); |
|
142
|
|
|
this.$addBlockBtnGroupBtns = this.$addBlockBtnGroup.children('.btn'); |
|
143
|
|
|
|
|
144
|
|
|
// Create our action button menus |
|
145
|
|
|
this.$blockContainer.find('> > .actions > .settings').each( (index, value) => { |
|
|
|
|
|
|
146
|
|
|
const $value = $(value); |
|
147
|
|
|
let menuBtn; |
|
148
|
|
|
if ($value.data('menubtn')) { |
|
149
|
|
|
menuBtn = $value.data('menubtn'); |
|
150
|
|
|
} else { |
|
151
|
|
|
menuBtn = new Garnish.MenuBtn(value); |
|
|
|
|
|
|
152
|
|
|
} |
|
153
|
|
|
menuBtn.menu.settings.onOptionSelect = $.proxy(function(option) { |
|
|
|
|
|
|
154
|
|
|
this.onMenuOptionSelect(option, menuBtn); |
|
155
|
|
|
}, this); |
|
|
|
|
|
|
156
|
|
|
}); |
|
|
|
|
|
|
157
|
|
|
|
|
158
|
|
|
const $blocks = this.$blockContainer.children(); |
|
159
|
|
|
|
|
160
|
|
|
this.blockSort = new Garnish.DragSort($blocks, { |
|
|
|
|
|
|
161
|
|
|
handle: '> .actions > .move', |
|
162
|
|
|
axis: 'y', |
|
163
|
|
|
collapseDraggees: true, |
|
164
|
|
|
magnetStrength: 4, |
|
165
|
|
|
helperLagBase: 1.5, |
|
166
|
|
|
helperOpacity: 0.9, |
|
167
|
|
|
onSortChange: $.proxy(function() { |
|
|
|
|
|
|
168
|
|
|
this.resetVariantBlockOrder(); |
|
169
|
|
|
}, this) |
|
|
|
|
|
|
170
|
|
|
}); |
|
|
|
|
|
|
171
|
|
|
|
|
172
|
|
|
this.addListener(this.$addBlockBtnGroupBtns, 'click', function() { |
|
|
|
|
|
|
173
|
|
|
this.addVariantBlock(null); |
|
174
|
|
|
}); |
|
|
|
|
|
|
175
|
|
|
|
|
176
|
|
|
this.addAspectRatioHandlers(); |
|
177
|
|
|
this.reIndexVariants(); |
|
178
|
|
|
}, |
|
179
|
|
|
|
|
180
|
|
|
onMenuOptionSelect: function(option, menuBtn) { |
|
|
|
|
|
|
181
|
|
|
const $option = $(option); |
|
182
|
|
|
const container = menuBtn.$btn.closest('.matrixblock'); |
|
183
|
|
|
|
|
184
|
|
|
switch ($option.data('action')) { |
|
185
|
|
|
case 'add': { |
|
|
|
|
|
|
186
|
|
|
this.addVariantBlock(container); |
|
187
|
|
|
break; |
|
188
|
|
|
} |
|
189
|
|
|
case 'delete': { |
|
190
|
|
|
if (!$option.hasClass('disabled')) { |
|
191
|
|
|
this.deleteVariantBlock(container); |
|
192
|
|
|
} |
|
193
|
|
|
break; |
|
194
|
|
|
} |
|
195
|
|
|
} |
|
196
|
|
|
}, |
|
197
|
|
|
|
|
198
|
|
|
getHiddenBlockCss: function($block) { |
|
|
|
|
|
|
199
|
|
|
return { |
|
200
|
|
|
opacity: 0, |
|
201
|
|
|
marginBottom: -($block.outerHeight()) |
|
202
|
|
|
}; |
|
203
|
|
|
}, |
|
204
|
|
|
|
|
205
|
|
|
// Re-index all of the variant blocks |
|
206
|
|
|
reIndexVariants: function() { |
|
|
|
|
|
|
207
|
|
|
this.$blockContainer = this.$container.children('.variant-blocks'); |
|
208
|
|
|
const $blocks = this.$blockContainer.children(); |
|
209
|
|
|
$blocks.each(function (index, value) { |
|
|
|
|
|
|
210
|
|
|
const variantIndex = index; |
|
211
|
|
|
const $value = $(value); |
|
212
|
|
|
const elements = $value.find('div .field, label, input, select'); |
|
213
|
|
|
|
|
214
|
|
|
// Re-index all of the element attributes |
|
215
|
|
|
$(elements).each(function (index, value) { |
|
|
|
|
|
|
216
|
|
|
// id attributes |
|
217
|
|
|
let str = $(value).attr('id'); |
|
218
|
|
|
if (str) { |
|
219
|
|
|
str = str.replace(/-([0-9]+)-/g, "-" + variantIndex +"-"); |
|
220
|
|
|
$(value).attr('id', str); |
|
221
|
|
|
} |
|
222
|
|
|
// for attributes |
|
223
|
|
|
str = $(value).attr('for'); |
|
224
|
|
|
if (str) { |
|
225
|
|
|
str = str.replace(/-([0-9]+)-/g, "-" + variantIndex +"-"); |
|
226
|
|
|
$(value).attr('for', str); |
|
227
|
|
|
} |
|
228
|
|
|
// Name attributes |
|
229
|
|
|
str = $(value).attr('name'); |
|
230
|
|
|
if (str) { |
|
231
|
|
|
str = str.replace(/\[([0-9]+)]/g, "[" + variantIndex +"]"); |
|
232
|
|
|
$(value).attr('name', str); |
|
233
|
|
|
} |
|
234
|
|
|
}); |
|
|
|
|
|
|
235
|
|
|
}); |
|
|
|
|
|
|
236
|
|
|
let disabledDeleteItem = false; |
|
237
|
|
|
// If we only have one block, don't allow it to be deleted |
|
238
|
|
|
if ($blocks.length == 1) { |
|
|
|
|
|
|
239
|
|
|
disabledDeleteItem = true; |
|
240
|
|
|
} |
|
241
|
|
|
$blocks.find('> .actions > .settings').each(function (index, value) { |
|
|
|
|
|
|
242
|
|
|
const $value = $(value); |
|
243
|
|
|
let menuBtn; |
|
244
|
|
|
if ($value.data('menubtn')) { |
|
245
|
|
|
menuBtn = $value.data('menubtn'); |
|
246
|
|
|
let menuItem = $(menuBtn.menu.$menuList[1]); |
|
247
|
|
|
if (typeof menuItem !== undefined) { |
|
248
|
|
|
if (disabledDeleteItem) { |
|
249
|
|
|
menuItem.find("> li > a").addClass('disabled').disable(); |
|
250
|
|
|
} else { |
|
251
|
|
|
menuItem.find("> li > a").removeClass('disabled').enable(); |
|
252
|
|
|
} |
|
253
|
|
|
} |
|
254
|
|
|
} |
|
255
|
|
|
}); |
|
|
|
|
|
|
256
|
|
|
}, |
|
257
|
|
|
|
|
258
|
|
|
addAspectRatioHandlers: function () { |
|
259
|
|
|
this.addListener($('.lightswitch'), 'click', function(ev) { |
|
|
|
|
|
|
260
|
|
|
const $target = $(ev.target); |
|
261
|
|
|
const $block = $target.closest('.matrixblock'); |
|
262
|
|
|
$block.find('.io-aspect-ratio-wrapper').slideToggle(); |
|
263
|
|
|
}); |
|
|
|
|
|
|
264
|
|
|
this.addListener($('.io-select-ar-box'), 'click', function(ev) { |
|
|
|
|
|
|
265
|
|
|
const $target = $(ev.target); |
|
266
|
|
|
let x = $(ev.target).data('x'), |
|
267
|
|
|
y = $(ev.target).data('y'), |
|
268
|
|
|
custom = $(ev.target).data('custom'), |
|
269
|
|
|
field, $block; |
|
270
|
|
|
// Select the appropriate aspect ratio |
|
271
|
|
|
$block = $target.closest('.matrixblock'); |
|
272
|
|
|
$block.find('.io-select-ar-box').each(function (index, value) { |
|
|
|
|
|
|
273
|
|
|
$(value).removeClass('io-selected-ar-box'); |
|
274
|
|
|
}); |
|
|
|
|
|
|
275
|
|
|
$target.addClass('io-selected-ar-box'); |
|
276
|
|
|
|
|
277
|
|
|
// Handle setting the actual field values |
|
278
|
|
|
if (custom) { |
|
279
|
|
|
$block.find('.io-custom-ar-wrapper').slideDown(); |
|
280
|
|
|
} else { |
|
281
|
|
|
$block.find('.io-custom-ar-wrapper').slideUp(); |
|
282
|
|
|
field = $block.find('input')[2]; |
|
283
|
|
|
$(field).val(x); |
|
284
|
|
|
field = $block.find('input')[3]; |
|
285
|
|
|
$(field).val(y); |
|
286
|
|
|
} |
|
287
|
|
|
}); |
|
|
|
|
|
|
288
|
|
|
}, |
|
289
|
|
|
|
|
290
|
|
|
addVariantBlock: function(container) { |
|
|
|
|
|
|
291
|
|
|
let $block = $(this.$blockContainer.children()[0]).clone(); |
|
292
|
|
|
// Reset to default values |
|
293
|
|
|
$block.find('.io-select-ar-box').each( (index, value) => { |
|
|
|
|
|
|
294
|
|
|
if (index === 0) { |
|
295
|
|
|
$(value).addClass('io-selected-ar-box'); |
|
296
|
|
|
} else { |
|
297
|
|
|
$(value).removeClass('io-selected-ar-box'); |
|
|
|
|
|
|
298
|
|
|
} |
|
299
|
|
|
}); |
|
|
|
|
|
|
300
|
|
|
$block.find('.io-custom-ar-wrapper').hide(); |
|
301
|
|
|
let field = $block.find('input')[0]; |
|
302
|
|
|
$(field).val(1200); |
|
303
|
|
|
field = $block.find('input')[1]; |
|
304
|
|
|
$(field).val(1); |
|
305
|
|
|
field = $block.find('input')[2]; |
|
306
|
|
|
$(field).val(16); |
|
307
|
|
|
field = $block.find('input')[3]; |
|
308
|
|
|
$(field).val(9); |
|
309
|
|
|
field = $block.find('select')[0]; |
|
310
|
|
|
$(field).val(82); |
|
311
|
|
|
field = $block.find('select')[1]; |
|
312
|
|
|
$(field).val('jpg'); |
|
313
|
|
|
$block.css(this.getHiddenBlockCss($block)).velocity({ |
|
|
|
|
|
|
314
|
|
|
opacity: 1, |
|
315
|
|
|
'margin-bottom': 10 |
|
316
|
|
|
}, 'fast', $.proxy(function() { |
|
|
|
|
|
|
317
|
|
|
// Insert the block in the right place |
|
318
|
|
|
if (container) { |
|
319
|
|
|
$block.insertBefore(container); |
|
320
|
|
|
} else { |
|
321
|
|
|
$block.appendTo(this.$blockContainer); |
|
322
|
|
|
} |
|
323
|
|
|
// Update the Garnish UI controls |
|
324
|
|
|
this.blockSort.addItems($block); |
|
325
|
|
|
this.addAspectRatioHandlers(); |
|
326
|
|
|
$block.find('.settings').each( (index, value) => { |
|
|
|
|
|
|
327
|
|
|
let $value = $(value), |
|
328
|
|
|
menuBtn, |
|
|
|
|
|
|
329
|
|
|
menu; |
|
|
|
|
|
|
330
|
|
|
|
|
|
|
|
|
|
331
|
|
|
menu = this.$container.find('.io-menu-clone > .menu').clone(); |
|
332
|
|
|
$(menu).insertAfter($value); |
|
333
|
|
|
menuBtn = new Garnish.MenuBtn(value); |
|
334
|
|
|
|
|
|
|
|
|
|
335
|
|
|
menuBtn.menu.settings.onOptionSelect = $.proxy(function(option) { |
|
|
|
|
|
|
336
|
|
|
this.onMenuOptionSelect(option, menuBtn); |
|
337
|
|
|
}, this); |
|
|
|
|
|
|
338
|
|
|
}); |
|
|
|
|
|
|
339
|
|
|
this.reIndexVariants(); |
|
340
|
|
|
}, this)); |
|
|
|
|
|
|
341
|
|
|
}, |
|
342
|
|
|
|
|
343
|
|
|
deleteVariantBlock: function(container) { |
|
|
|
|
|
|
344
|
|
|
container.velocity(this.getHiddenBlockCss(container), 'fast', $.proxy( () => { |
|
|
|
|
|
|
345
|
|
|
container.remove(); |
|
346
|
|
|
this.reIndexVariants(); |
|
347
|
|
|
}, this)); |
|
|
|
|
|
|
348
|
|
|
}, |
|
349
|
|
|
|
|
350
|
|
|
resetVariantBlockOrder: function() { |
|
|
|
|
|
|
351
|
|
|
this.reIndexVariants(); |
|
352
|
|
|
} |
|
353
|
|
|
}); |
|
|
|
|
|
|
354
|
|
|
|